﻿using Styx;
using Styx.Common;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BagSpaceCreator
{
    public partial class Form1 : Form
    {
        private string _SavePathDelete = Path.Combine(Utilities.AssemblyDirectory, string.Format(@"Plugins/PluginSettings/Pasterke/DeleteItems-{0}-{1}.txt", StyxWoW.Me.RealmName, StyxWoW.Me.Name));
        private string _SavePathTen = Path.Combine(Utilities.AssemblyDirectory, string.Format(@"Plugins/PluginSettings/Pasterke/UseOnTenList.txt"));
        private string _SavePathFive = Path.Combine(Utilities.AssemblyDirectory, string.Format(@"Plugins/PluginSettings/Pasterke/UseOnFiveList.txt"));
        private string _SavePathThree = Path.Combine(Utilities.AssemblyDirectory, string.Format(@"Plugins/PluginSettings/Pasterke/UseOnThreeList.txt"));
        private string _SavePathOne = Path.Combine(Utilities.AssemblyDirectory, string.Format(@"Plugins/PluginSettings/Pasterke/UseOnOneList.txt"));
        private string _writePath = Path.Combine(Utilities.AssemblyDirectory, string.Format(@"Plugins/PluginSettings/Pasterke/"));

        private List<string> _deleteList = new List<string>();
        private List<string> _useOnTenList = new List<string>();
        private List<string> _newUseOnTenList = new List<string>();
        private List<string> _useOnFiveList = new List<string>();
        private List<string> _newUseOnFiveList = new List<string>();
        private List<string> _useOnThreeList = new List<string>();
        private List<string> _newUseOnThreeList = new List<string>();
        private List<string> _useOnOneList = new List<string>();
        private List<string> _newUseOnOneList = new List<string>();
        private List<string> _myBagList = new List<string>();
        private List<string> _newRemoveList = new List<string>();
        private List<string> _removeList = new List<string>();

        public Form1()
        {
            InitializeComponent();

            checkBox1.Checked = SettingsBagSpaceCreator.myPrefs.DeleteGreyItems;
            checkBox2.Checked = SettingsBagSpaceCreator.myPrefs.DeleteWhiteItems;
            checkBox3.Checked = SettingsBagSpaceCreator.myPrefs.DeleteGreenItems;
            checkBox4.Checked = SettingsBagSpaceCreator.myPrefs.DeleteBlueItems;
            checkBox5.Checked = SettingsBagSpaceCreator.myPrefs.DEGreenBOEItems;
            checkBox6.Checked = SettingsBagSpaceCreator.myPrefs.DEGreenSoulboundItems;
            checkBox6.Checked = SettingsBagSpaceCreator.myPrefs.DEBlueBOEItems;
            checkBox7.Checked = SettingsBagSpaceCreator.myPrefs.DEBlueSoulboundItems;


            CreateDirIfMissing();
            PopulateBagItems();
            PopulateToDestroy();
            PopulateUseOnFive();
            PopulateUseOnOne();
            PopulateUseOnThree();
            PopulateUseOnTen();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void PopulateBagItems()
        {
            foreach (WoWItem bagItem in StyxWoW.Me.BagItems)
            {
                if (bagItem.IsValid && bagItem.BagSlot != -1 && !BagItems.Items.Contains(bagItem.Name)) 
                {
                    BagItems.Items.Add(bagItem.Name);
                }
            }
        }

        private void PopulateUseOnOne()
        {
            if (!File.Exists(_SavePathOne))
            {
                File.Create(_SavePathOne);
            }
            string f = _SavePathOne;

            using (StreamReader r = new StreamReader(f))
            {
                string line;
                while ((line = r.ReadLine()) != null)
                {
                    _useOnOneList.Add(line);
                }
            }
            _useOnOneList.Sort();

            foreach (string listItem in _useOnOneList)
            {
                UseOnOne.Items.Add(listItem);
            }
        }

        private void PopulateUseOnThree()
        {
            if (!File.Exists(_SavePathThree))
            {
                File.Create(_SavePathThree);
            }
            string f = _SavePathThree;

            using (StreamReader r = new StreamReader(f))
            {
                string line;
                while ((line = r.ReadLine()) != null)
                {
                    _useOnThreeList.Add(line);
                }
            }
            _useOnThreeList.Sort();

            foreach (string listItem in _useOnThreeList)
            {
                UseOnThree.Items.Add(listItem);
            }
        }

        private void PopulateUseOnFive()
        {
            if (!File.Exists(_SavePathFive))
            {
                File.Create(_SavePathFive);
            }
            string f = _SavePathFive;

            using (StreamReader r = new StreamReader(f))
            {
                string line;
                while ((line = r.ReadLine()) != null)
                {
                    _useOnFiveList.Add(line);
                }
            }
            _useOnFiveList.Sort();

            foreach (string listItem in _useOnFiveList)
            {
                UseOnFive.Items.Add(listItem);
            }
        }

        private void PopulateUseOnTen()
        {
            if (!File.Exists(_SavePathTen))
            {
                File.Create(_SavePathTen);
            }
            string f = _SavePathTen;

            using (StreamReader r = new StreamReader(f))
            {
                string line;
                while ((line = r.ReadLine()) != null)
                {
                    _useOnTenList.Add(line);
                }
            }
            _useOnTenList.Sort();

            foreach (string listItem in _useOnTenList)
            {
                UseOnTen.Items.Add(listItem);
            }
        }

        private void PopulateToDestroy()
        {
            if (!File.Exists(_SavePathDelete))
            {
                File.Create(_SavePathDelete);
            }
            string f = _SavePathDelete;

            using (StreamReader r = new StreamReader(f))
            {
                string line;
                while ((line = r.ReadLine()) != null)
                {
                    _removeList.Add(line);
                }
            }
            _removeList.Sort();

            foreach (string listItem in _removeList)
            {
                DestroyItems.Items.Add(listItem);
            }
        }

        private void ClearDestroyList()
        {
            foreach (string listItem in _removeList)
            {
                DestroyItems.Items.Remove(listItem);
            }
        }

        private void CreateDirIfMissing()
        {
            bool folderExists = Directory.Exists(_writePath);
            if (!folderExists)
                Directory.CreateDirectory(_writePath);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            SettingsBagSpaceCreator.myPrefs.Save();

            PrepareToWriteNewFiles();

            TextWriter tw = new StreamWriter(_SavePathDelete);

            foreach (String s in _newRemoveList)
            {
                tw.WriteLine(s);
            }
            tw.Close();

            TextWriter one = new StreamWriter(_SavePathOne);
            foreach (string s in _newUseOnOneList)
            {
                one.WriteLine(s);
            }
            one.Close();

            TextWriter three = new StreamWriter(_SavePathThree);
            foreach (string s in _newUseOnThreeList)
            {
                three.WriteLine(s);
            }
            three.Close();

            TextWriter five = new StreamWriter(_SavePathFive);
            foreach (string s in _newUseOnFiveList)
            {
                five.WriteLine(s);
            }
            five.Close();

            TextWriter ten = new StreamWriter(_SavePathTen);
            foreach (string s in _newUseOnTenList)
            {
                ten.WriteLine(s);
            }
            ten.Close();

            Close();
        }

        private void PrepareToWriteNewFiles()
        {
            foreach (string listItem in DestroyItems.Items)
            {
                _newRemoveList.Add(listItem);
            }
            foreach (string listItem in UseOnOne.Items)
            {
                _newUseOnOneList.Add(listItem);
            }
            foreach (string listItem in UseOnThree.Items)
            {
                _newUseOnThreeList.Add(listItem);
            }
            foreach (string listItem in UseOnFive.Items)
            {
                _newUseOnFiveList.Add(listItem);
            }
            foreach (string listItem in UseOnTen.Items)
            {
                _newUseOnTenList.Add(listItem);
            }
            _newRemoveList.Sort();
            _newUseOnFiveList.Sort();
            _newUseOnOneList.Sort();
            _newUseOnTenList.Sort();
            _newUseOnThreeList.Sort();
        }

        private void BagToDestroy_Click(object sender, EventArgs e)
        {
            DestroyItems.Items.Add(BagItems.SelectedItem);
            _removeList.Add(BagItems.SelectedItem.ToString());
        }

        private void BagToOne_Click(object sender, EventArgs e)
        {
            UseOnOne.Items.Add(BagItems.SelectedItem);
            _useOnOneList.Add(BagItems.SelectedItem.ToString());
        }

        private void BagToThree_Click(object sender, EventArgs e)
        {
            UseOnThree.Items.Add(BagItems.SelectedItem);
            _useOnThreeList.Add(BagItems.SelectedItem.ToString());
        }

        private void BagToFive_Click(object sender, EventArgs e)
        {
            UseOnFive.Items.Add(BagItems.SelectedItem);
            _useOnFiveList.Add(BagItems.SelectedItem.ToString());
        }

        private void BagToTen_Click(object sender, EventArgs e)
        {
            UseOnTen.Items.Add(BagItems.SelectedItem);
            _useOnTenList.Add(BagItems.SelectedItem.ToString());
        }

        private void RemoveFromOne_Click(object sender, EventArgs e)
        {
            UseOnOne.Items.Remove(UseOnOne.SelectedItem);
        }

        private void RemoveFromThree_Click(object sender, EventArgs e)
        {
            UseOnThree.Items.Remove(UseOnThree.SelectedItem);
        }

        private void RemoveFromFive_Click(object sender, EventArgs e)
        {
            UseOnFive.Items.Remove(UseOnFive.SelectedItem);
        }

        private void RemoveFromTen_Click(object sender, EventArgs e)
        {
            UseOnTen.Items.Remove(UseOnTen.SelectedItem);
        }

        private void RemoveFromDestroy_Click(object sender, EventArgs e)
        {
            DestroyItems.Items.Remove(DestroyItems.SelectedItem);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            SettingsBagSpaceCreator.myPrefs.DeleteGreyItems = checkBox1.Checked;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            SettingsBagSpaceCreator.myPrefs.DeleteWhiteItems = checkBox2.Checked;
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            SettingsBagSpaceCreator.myPrefs.DeleteGreenItems = checkBox3.Checked;
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            SettingsBagSpaceCreator.myPrefs.DeleteBlueItems = checkBox4.Checked;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ClearDestroyList();
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            SettingsBagSpaceCreator.myPrefs.DEGreenBOEItems = checkBox5.Checked;
        }

        private void checkBox8_CheckedChanged(object sender, EventArgs e)
        {
            SettingsBagSpaceCreator.myPrefs.DEGreenSoulboundItems = checkBox6.Checked;
        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
            SettingsBagSpaceCreator.myPrefs.DEBlueBOEItems = checkBox6.Checked;
        }

        private void checkBox7_CheckedChanged(object sender, EventArgs e)
        {
            SettingsBagSpaceCreator.myPrefs.DEBlueSoulboundItems = checkBox7.Checked;
        }

    }
}
